package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysErcAddress;
import cn.wizzer.app.sys.modules.services.SysErcAddressService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/ercAddress")
public class SysErcAddressController {
    private static final Log log = Logs.get();
    @Inject
    @Reference
    private SysErcAddressService sysErcAddressService;
    @Inject
    @Reference
    private UserService userService;

    @At("")
    @Ok("beetl:/platform/sys/ercAddress/index.html")
    @RequiresPermissions("sys.manager.ercAddress")
    public void index() {

    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.ercAddress")
    public Object data(Integer pageNumber, int pageSize) {
        Cnd cnd = Cnd.NEW();
        cnd.and("delFlag","=",0);
        cnd.orderBy("ctAt","desc");
        return Result.success().addData(sysErcAddressService.listPage(pageNumber,pageSize,cnd));
    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress.add")
    @SLog(tag = "新增收款地址", msg = "地址：${args[0].address}")
    public Object add(@Param("..") SysErcAddress ercAddress) {
        sysErcAddressService.add(ercAddress);
        return Result.success();
    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress.del")
    @SLog(tag = "删除收款地址", msg = "地址ID：${args[0]}")
    public Object del(String id) {
        sysErcAddressService.vDel(id);
        return Result.success();
    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress.edit")
    @SLog(tag = "禁用收款地址", msg = "地址ID：${args[0]}")
    public Object disable(String id) {
        sysErcAddressService.update(Chain.make("disabled",1),Cnd.where("id","=",id));
        sysErcAddressService.clearCache();
        return Result.success();
    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress.edit")
    @SLog(tag = "启用收款地址", msg = "地址ID：${args[0]}")
    public Object enable(String id) {
        sysErcAddressService.update(Chain.make("disabled",0),Cnd.where("id","=",id));
        sysErcAddressService.clearCache();
        return Result.success();
    }
    @At("/edit/?")
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress")
    public Object edit(String id) {
        return Result.successNonNull(sysErcAddressService.fetch(id));
    }
    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.ercAddress.edit")
    @SLog(tag = "修改收款地址", msg = "地址ID：${args[0].id},备注：${args[0].remark}")
    public Object editDo(@Param("..")SysErcAddress address) {
        address.setOpBy(StringUtil.getPlatformUid());
        address.setOpAt(1L);
        sysErcAddressService.update(address);
        sysErcAddressService.clearCache();
        return Result.success();
    }


}
